package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroCoupons;
import entity.CroCustomer;
import entity.CroMyqpon;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroCouponsService;
import service.CroCustomerService;
import service.CroMyqponService;
import vo.ShowCouponsVo;

import javax.servlet.ServletContext;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/21 0021.
 */
public class MycouponsAction extends ActionSupport {
    @Autowired
    private CroCouponsService croCouponsService;
    @Autowired
    private CroMyqponService croMyqponService;
    private CroCustomer croCustomer;
    private ShowCouponsVo couponsVo;
    private ShowCouponsVo recouponsVo;
    private CroMyqpon croMyqpon;
    private CroCoupons coupons;
    private List<CroCoupons> croCoupons;
    private List<CroMyqpon> croMyqpons;
    private String cpid;
    private String mycpid;
    private String successResult;
    private int number;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private String jsonObject;

    /*分页*/
    public String showdetail(){
        Pagination<CroCoupons> page;
        if(pageIndex==0){
            page=croCouponsService.findCoupons(1,pageSize);
        }else{
            page=croCouponsService.findCoupons(pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroCoupons(page.getItems());
        return SUCCESS;
    }

    /*分页查询个人优惠券*/
    public String showdetailbyMycoupons(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer=(CroCustomer) context.getAttribute("customer");
        System.out.println(customer.getId());
        Pagination<CroMyqpon> page;
        if(pageIndex==0){
            page=croMyqponService.findAllCoupons(1,pageSize,customer.getId());
        }else{
            page=croMyqponService.findAllCoupons(pageIndex,pageSize,customer.getId());
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroMyqpons(page.getItems());
        return "Mycoupon";
    }

    /*分页查询*/
    public String querylist(){
        Pagination<CroCoupons> page;
        if(pageIndex==0){
            page=croCouponsService.FindCouponsByProperty(couponsVo,1,pageSize);
        }else {
            page=croCouponsService.FindCouponsByProperty(couponsVo,pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setRecouponsVo(couponsVo);
        jsonObject= JSON.toJSONString(recouponsVo);
        setCroCoupons(page.getItems());
        return SUCCESS;
    }


    /*领取优惠券*/
    public String receive(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer customer=(CroCustomer) context.getAttribute("customer");
        System.out.println(customer);
        CroCoupons coupons = croCouponsService.FindCouponsByid(cpid);

        if(coupons.getNumber()>0) {
            CroMyqpon croMyqpon = new CroMyqpon();
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
            dateFormat.format(date);
            int number = coupons.getNumber() - 1;
            coupons.setNumber(number);
            //修改总仓数量

            croMyqpon.setCroCustomer(customer);
            croMyqpon.setCroCoupons(coupons);
            croMyqpon.setOffdate(coupons.getOffdate());
            croMyqpon.setCreatedate(date);

            if (croMyqponService.checkCoupons(cpid, customer.getId())) {
                croCouponsService.issueCoupons(coupons);
                croMyqponService.addMyqponByEntity(croMyqpon);//保存到个人优惠券
                successResult = "领取成功";
            } else {
                successResult = "领取失败";
            }
        }else{
            successResult = "优惠券不足";
        }
        return SUCCESS;
    }

    /*删除*/
    public String delete(){
        CroMyqpon croMyqpon= croMyqponService.FindMyqponbyid(mycpid);
        croMyqpon.setDel_flag("1");
        if (croMyqponService.updateMyqponByEntity(croMyqpon)){
            successResult = "删除成功";
        }else {
            successResult = "删除失败";
        }
        return SUCCESS;
    }

    public String getMycpid() {
        return mycpid;
    }

    public void setMycpid(String mycpid) {
        this.mycpid = mycpid;
    }

    public List<CroMyqpon> getCroMyqpons() {
        return croMyqpons;
    }

    public void setCroMyqpons(List<CroMyqpon> croMyqpons) {
        this.croMyqpons = croMyqpons;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public CroCustomer getCroCustomer() {
        return croCustomer;
    }

    public void setCroCustomer(CroCustomer croCustomer) {
        this.croCustomer = croCustomer;
    }

    public CroMyqpon getCroMyqpon() {
        return croMyqpon;
    }

    public void setCroMyqpon(CroMyqpon croMyqpon) {
        this.croMyqpon = croMyqpon;
    }

    public CroCoupons getCoupons() {
        return coupons;
    }

    public void setCoupons(CroCoupons coupons) {
        this.coupons = coupons;
    }

    public ShowCouponsVo getCouponsVo() {
        return couponsVo;
    }

    public void setCouponsVo(ShowCouponsVo couponsVo) {
        this.couponsVo = couponsVo;
    }

    public ShowCouponsVo getRecouponsVo() {
        return recouponsVo;
    }

    public void setRecouponsVo(ShowCouponsVo recouponsVo) {
        this.recouponsVo = recouponsVo;
    }

    public String getCpid() {
        return cpid;
    }

    public void setCpid(String cpid) {
        this.cpid = cpid;
    }

    public List<CroCoupons> getCroCoupons() {
        return croCoupons;
    }

    public void setCroCoupons(List<CroCoupons> croCoupons) {
        this.croCoupons = croCoupons;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(String jsonObject) {
        this.jsonObject = jsonObject;
    }
}
